<?php

namespace app\stock\controller\business;

use think\Controller;

//私海
class Privatesea extends Controller
{
  public function _initialize()
  {
    parent::_initialize();
    $this->BusinessModel = model('Business.Business');
    $this->AdminModel = model('Admin');
    $this->ReceiveModel = model('Business.Receive');
  }

  // 列表
  public function index()
  {
    $adminid = $this->request->param('adminid', 0, 'trim'); //管理员id
    $admininfo = $this->AdminModel->find($adminid);
    if (!$admininfo) {
      $this->error('管理员不存在');
    }
    $data = $this->BusinessModel->with('source')->where('adminid', $adminid)->order('createtime DESC')->select();
    $this->success('', '', $data);
  }

  // 详情
  public function info()
  {
    $busid = $this->request->param('busid', 0, 'trim');
    // $businessinfo = $this->BusinessModel->field('id,nickname,gender,deal,createtime')->with('source')->find($busid);
    $businessinfo = $this->BusinessModel->with('source')->find($busid);

    if (!$businessinfo) {
      $this->success('当前用户不存在');
    }

    $this->success('', '', $businessinfo);
  }

  //编辑
  public function edit()
  {
    $busid = $this->request->param('busid', '', 'trim');
    $nickname = $this->request->param('nickname', '', 'trim');
    $gender = $this->request->param('gender', 0, 'trim');
    $sourceid = $this->request->param('sourceid', 0, 'trim');
    $deal = $this->request->param('deal', 0, 'trim');
    /* halt(
      $busid.','.
      $nickname.','.
      $gender.','.
      $sourceid.','.
      $deal
    ); */
    $businessinfo = $this->BusinessModel->find($busid);
    if (!$businessinfo) {
      $this->error('管理员不存在');
    }

    //可以加校验器！！！
    $businessStatus = $this->BusinessModel->where('id', $busid)->update(['nickname' => $nickname, 'gender' => $gender, 'sourceid' => $sourceid, 'deal' => $deal]);

    if ($businessStatus == false) {
      $this->error('修改失败');
    } else {
      $this->success('修改成功');
    }
  }

  // 管理员列表
  /*   public function admin()
  {
    $data = $this->AdminModel->field('id,username')->order('createtime DESC')->select();

    $this->success('', '', $data);
  }
 */

  // 分配 和 申请 
  /*  public function allot()
  {
    $busid = $this->request->param('busid', 0, 'trim');
    $adminid = $this->request->param('adminid', 0, 'trim');
    $status = $this->request->param('status', '', 'trim');

    $businessinfo = $this->BusinessModel->find($busid);

    if (!$businessinfo) {
      $this->error('当前用户不存在');
    }

    $admininfo = $this->AdminModel->find($adminid);

    if (!$admininfo) {
      $this->error('分配人员不存在');
    }

    // 开始事务
    $this->BusinessModel->startTrans();
    $this->ReceiveModel->startTrans();

    $businessStatus = $this->BusinessModel->where('id', $busid)->update(['adminid' => $adminid]);

    if ($businessStatus == false) {
      $this->error($this->BusinessModel->geError());
    }

    $ReceiveData = [
      'applyid' => $adminid,
      'status' => $status,
      'busid' => $busid
    ];

    // 校验器
    $receiveStatus = $this->ReceiveModel->save($ReceiveData);
    if ($receiveStatus == false) {
      $this->BusinessModel->rollback();
      $this->error($this->ReceiveModel->geError());
    }

    if ($receiveStatus == false || $businessStatus == false) {
      $this->BusinessModel->rollback();
      $this->ReceiveModel->rollback();
      $this->error('操作失败');
    } else {
      $this->BusinessModel->commit();
      $this->ReceiveModel->commit();
      $this->success('操作成功');
    }
  } */

  //回收
  public function recovery($ids = null)
  {
    $busid = $this->request->param('busid', 0, 'trim');
    $adminid = $this->request->param('adminid', 0, 'trim');
    $result = $this->BusinessModel->where(['id' => $busid, 'adminid' => $adminid])->update(['adminid' => Null]);
    if ($result == false) {
      $this->error('回收失败');
    } else {
      $this->success('回收成功');
    }
  }

  // 删除
  public function del()
  {
    // halt('回到公海还是软删除！！！');
    $busid = $this->request->param('busid', 0, 'trim');
    $adminid = $this->request->param('adminid', 0, 'trim');

    $businessinfo = $this->BusinessModel->find($busid);

    if (!$businessinfo) {
      $this->error('当前用户不存在');
    }

    $admininfo = $this->AdminModel->find($adminid);

    if (!$admininfo) {
      $this->error('管理员不存在');
    }

    $result = $this->BusinessModel->destroy($busid); //软删除

    if ($result === false) {
      $this->error('删除失败');
    } else {
      $this->success('删除成功');
    }
  }
}
